<template>
  <div>
    <div class="header">
      <van-icon class="back-btn" name="arrow-left" @click="$router.go(-1)" />
      <div class="title">客服聊天</div>
    </div>
    <div class="container" ref="msgContent">
      <ul class="container-ul">
        <li
          class="msg-item"
          v-for="(item, index) in msgArr"
          :key="index"
          :class="item.status ? 'msg-right' : 'msg-left'"
        >
          <!-- 对方 -->
          <template v-if="item.status">
            <img src="./images/kefu.jpeg" alt="" />
            <div
              class="msg-box"
              :class="item.status ? 'msg-box-left' : 'msg-box-right'"
            >
              {{ item.msg }}
            </div>
          </template>
          <!-- 自己 -->
          <template v-else>
            <div
              class="msg-box"
              :class="item.status ? 'msg-box-left' : 'msg-box-right'"
            >
              {{ item.msg }}
            </div>
            <img src="./images/pic.jpeg" alt="" />
          </template>
        </li>
      </ul>
    </div>
    <div class="footer">
      <input
        class="ipt"
        type="text"
        placeholder="请输入要发送的消息"
        v-model="message"
      />
      <div class="send-btn" @click="sendMessage">发送</div>
    </div>
  </div>
</template>

<script>
var ws = new WebSocket("ws://localhost:5000");
import { Toast } from "vant";
export default {
  data() {
    return {
      msgArr: [],
      message: "",
    };
  },
  mounted() {
    var ws = new WebSocket("ws://localhost:5000");
    ws.onopen = () => {
      // console.log("前端好了");
    };
    ws.onmessage = (res) => {
      this.msgArr.push(JSON.parse(res.data));
    };
  },
  methods: {
    sendMessage() {
      if (this.message === "") {
        Toast("消息不能为空哦!");
        return;
      }
      this.msgArr.push({
        status: 0,
        msg: this.message,
      });
      // 消息自动滚动到底部
      this.$refs.msgContent.scrollTop = this.$refs.msgContent.scrollHeight;
      let id = this.$store.state.login.userInfo.id;
      let msgObj = {
        msg: this.message,
        id,
      };
      ws.send(JSON.stringify(msgObj));
      this.message = "";
    },
  },
  watch: {
    msgArr() {
      this.$nextTick(() => {
        ws.onmessage = (res) => {
          this.msgArr.push(JSON.parse(res.data));
        };
        this.$refs.msgContent.scrollTop = this.$refs.msgContent.scrollHeight;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 50px;
  background-color: #fff;
  position: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  .back-btn {
    position: absolute;
    left: 10px;
    font-size: 18px;
  }
}
.container {
  height: 100vh;
  overflow: auto;
  box-sizing: border-box;
  padding-top: 60px;
  padding-bottom: 60px;
  ul {
    .msg-item {
      min-height: 60px;
      width: 100%;
      margin-bottom: 10px;
      padding: 10px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
      .msg-box {
        padding: 10px;
        min-height: 20px;
        max-width: 200px;
        line-height: 20px;
        white-space: pre-wrap;
        word-break: break-all;
        word-wrap: break-word;
        border-radius: 10px;
      }
      .msg-box-left {
        background-color: #fff;
      }
      .msg-box-right {
        background-color: skyblue;
      }
    }
    .msg-right {
      img {
        margin-right: 10px;
      }
    }
    .msg-left {
      justify-content: flex-end;
      img {
        margin-left: 10px;
      }
    }
  }
}
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  padding: 10px;
  box-sizing: border-box;
  background-color: #f8f8f8;
  box-shadow: 0 1px 6px 2px rgb(0 0 0 / 20%);
  display: flex;
  .ipt {
    flex: 1;
    border-radius: 20px;
    border: none;
    padding-left: 10px;
    margin-right: 10px;
  }
  .send-btn {
    width: 70px;
    height: 40px;
    background-color: #143ea9;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 20px;
  }
}
</style>
